package com.fbt.energy.util;

import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

/**
 * @author Shun yuan
 * @date 2022年01月17日 15:46
 */
@Component
public class GetNumberOfDays {

    /**根据年份获取下面的所有月*/
    public static String[] getYearFullMonth(String year){
        String[] array = new String[12];
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR,Integer.parseInt(year));
        cal.set(Calendar.MONTH, 0);
        for(int i=0;i<12;i++){
            array[i] = sdf.format(cal.getTime());
            cal.add(Calendar.MONTH, 1);
        }
        return array;
    }

    /**根据月份获取每日*/
    public static List<String> getDayByMonth(int yearParam, int monthParam){
        List<String> list = new ArrayList<>();
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.set(yearParam, monthParam-1, 1);
        int year = calendar.get(Calendar.YEAR);//年份
        int month = calendar.get(Calendar.MONTH) + 1;//月份
        int day = calendar.getActualMaximum(Calendar.DATE);
        for (int i = 1; i <= day; i++) {
            String date=null;
            if(month<10 && i<10){
                date = String.valueOf(year)+"-0"+month+"-0"+i;
            }
            if(month<10 && i>=10){
                date = String.valueOf(year)+"-0"+month+"-"+i;
            }
            if(month>=10 && i<10){
                date = String.valueOf(year)+"-"+month+"-0"+i;
            }
            if(month>=10 && i>=10){
                date = String.valueOf(year)+"-"+month+"-"+i;
            }

            list.add(date);
        }
        return list;
    }
}
